/*
 * Copyright (c) 2018-2025, duneng All rights reserved.
 *
 * Author: wangsj (wsjlb4131214@126.com)
 */

/**
 *
 * http配置
 *
 */
/* eslint-disable */
import axios from 'axios'
import store from '../store'
import router from '../router/router'
import { getToken, setToken, removeToken } from '@/util/auth'
import { Message } from 'element-ui'
import errorCode from '@/const/errorCode'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// 超时时间
axios.defaults.timeout = 30000
    // 跨域请求，允许保存cookie
axios.defaults.withCredentials = true
//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
NProgress.configure({ showSpinner: false }) // NProgress Configuration
let msg
    // HTTPrequest拦截
axios.interceptors.request.use(config => {
        NProgress.start() // start progress bar
        if (store.getters.access_token) {
            config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
        }
        return config
    }, error => {
        return Promise.reject(error)
    })
    // HTTPresponse拦截
axios.interceptors.response.use(data => {
    NProgress.done()
    return data
}, error => {
    NProgress.done()
    let errMsg = error.toString()
    let code = errMsg.substr(errMsg.indexOf('code') + 5)
    Message({
        message: errorCode[code] || errorCode['default'],
        type: 'error'
    })
    return Promise.reject(new Error(error))
})

export default axios